package com.example.common.response;

import com.fasterxml.jackson.annotation.JsonFormat;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;

import java.io.Serializable;
import java.util.Date;

/**
 * Created with IntelliJ IDEA.
 * author: zjl
 * Date: 2020/9/3
 * Time: 11:31
 */
@Data
@ApiModel("通用响应对象")
public class ServerResponse<T> implements Serializable {

    private static final long serialVersionUID = 44321346635165465L;
    @ApiModelProperty("响应码")
    private int resultCode;
    @ApiModelProperty("响应信息")
    private String resultMsg;
    @ApiModelProperty("响应数据")
    private T resultObj;

    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss",timezone="GMT+8")
    @ApiModelProperty("响应时间")
    private Date timestamp;

    public static <T> ServerResponse<T> createBySuccess() {
        ServerResponse<T> serverResponse = new ServerResponse<>();
        serverResponse.setResultCode(ServerCode.SUCCESS.getCode());
        serverResponse.setResultMsg(ServerCode.SUCCESS.getDesc());
        serverResponse.setTimestamp(new Date());
        return serverResponse;
    }

    public static <T> ServerResponse<T> createBySuccessMessage(String resultMsg) {
        ServerResponse<T> serverResponse = ServerResponse.createBySuccess();
        serverResponse.setResultMsg(resultMsg);
        return serverResponse;
    }

    public static <T> ServerResponse<T> createBySuccess(T resultObj) {
        ServerResponse<T> serverResponse = ServerResponse.createBySuccess();
        serverResponse.setResultObj(resultObj);
        return serverResponse;
    }

    public static <T> ServerResponse<T> createBySuccess(String resultMsg, T resultObj) {
        ServerResponse<T> serverResponse = ServerResponse.createBySuccess();
        serverResponse.setResultMsg(resultMsg);
        serverResponse.setResultObj(resultObj);
        return serverResponse;
    }

    public static <T> ServerResponse<T> createByNoData() {
        ServerResponse<T> serverResponse = new ServerResponse<>();
        serverResponse.setResultCode(ServerCode.NO_DATA.getCode());
        serverResponse.setResultMsg(ServerCode.NO_DATA.getDesc());
        serverResponse.setTimestamp(new Date());
        return serverResponse;
    }


    public static <T> ServerResponse<T> createByError() {
        ServerResponse<T> serverResponse = new ServerResponse<>();
        serverResponse.setResultCode(ServerCode.SYSTEM_ERROR.getCode());
        serverResponse.setResultMsg(ServerCode.SYSTEM_ERROR.getDesc());
        serverResponse.setTimestamp(new Date());
        return serverResponse;
    }

    public static <T> ServerResponse<T> createByErrorMessage(int errorCode,String errorMessage) {
        ServerResponse<T> serverResponse = new ServerResponse<>();
        serverResponse.setResultCode(errorCode);
        serverResponse.setResultMsg(errorMessage);
        serverResponse.setTimestamp(new Date());
        return serverResponse;
    }

    public static <T> ServerResponse<T> createByErrorCodeMessage(ServerCode serverCode) {
        ServerResponse<T> serverResponse = new ServerResponse<>();
        serverResponse.setResultCode(serverCode.getCode());
        serverResponse.setResultMsg(serverCode.getDesc());
        serverResponse.setTimestamp(new Date());
        return serverResponse;
    }

}
